# frozen_string_literal: true

class RuleEntityService < ApplicationService
  class << self
    def create_draft(params)
      draft_options = { status: RuleEntity::Status::CREATED, stage: RuleEntity::Stage::DRAFT }
      entity = RuleEntity.new draft_options.merge(params.slice(:rule_id, :type, :policy))

      entity.rule_schema = RuleSchema.new schema_id: params[:schema_id] if params[:schema_id].present?

      entity.save!
      entity
    end

    def fire(id, inputs: {})
      entity = RuleEntity.find(id)

      entity.fire(inputs)
    end

    def deploy(id)
      entity = RuleEntity.find(id)

      entity.deploy
    end

    def validate(id)
      entity = RuleEntity.find(id)

      entity.validate
    end
  end
end
